package com.shadow.demo.dictionary.common.data;


import java.net.NetworkInterface;
import java.util.List;

/**
 * 上下文持有者
 *
 * @author penggch
 *
 */
public class ContextHolder {

    private boolean initialized = false;

    private String serverScheme;

    private String serverHost;

    private int serverPort;

    private String contextPath;

    private boolean securityEnabled = false;

    private String superAdmin;

    private String superPassword;

    public void initialize() {
        serverHost = getAvailableHost();
        initialized = true;
    }

    public boolean isInitialized() {
        return initialized;
    }

    public void setInitialized(boolean initialized) {
        this.initialized = initialized;
    }

    public String getServerScheme() {
        return serverScheme;
    }

    public void setServerScheme(String serverScheme) {
        this.serverScheme = serverScheme;
    }

    public String getServerHost() {
        return serverHost;
    }

    public void setServerHost(String serverHost) {
        this.serverHost = serverHost;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }

    public String getContextPath() {
        return contextPath;
    }

    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    public boolean isSecurityEnabled() {
        return securityEnabled;
    }

    public void setSecurityEnabled(boolean securityEnabled) {
        this.securityEnabled = securityEnabled;
    }

    public String getSuperAdmin() {
        return superAdmin;
    }

    public void setSuperAdmin(String superAdmin) {
        this.superAdmin = superAdmin;
    }

    public String getSuperPassword() {
        return superPassword;
    }

    public void setSuperPassword(String superPassword) {
        this.superPassword = superPassword;
    }

    private String getAvailableHost() {
//        if (serverHost != null && serverHost.length() > 0 && SocketUtil.testConnect2(serverHost, serverPort)) {
//            return serverHost;
//        }
//        String hostname = HardUtil.getHostName();
//        if (hostname != null && hostname.length() > 0 && SocketUtil.testConnect2(hostname, serverPort)) {
//            return hostname;
//        }
//
//        List<NetworkInterface> niList = HardUtil.getNetworkInfo();
//        for (NetworkInterface ni : niList) {
//            hostname = ni.getInetAddresses().nextElement().getHostAddress();
//            if (SocketUtil.testConnect2(hostname, serverPort)) {
//                return hostname;
//            }
//        }
        return null;
    }

    public boolean isCookieSecure() {
        return "https".equals(serverScheme);
    }

}
